/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLTriset.h"



//*********************************************
//** Exported Functions
//*********************************************
MDLLFUN bool CGLTrisetLoad(int trisetid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// Remove Triset if it exists already in table
	if(GlobalTrisetList.IsKey(trisetid)==true)
		{
		GlobalTextureList.Remove(trisetid);
		MStdPrintf("Removing existing Triset %d\n",trisetid);
		}

	// Load up the file from memory

	TRefCount<MCGLTriangleSelectorInfo> *newtriset=new TRefCount<MCGLTriangleSelectorInfo>;
	if(newtriset==NULL)
		{
		MStdPrintf("**Unable to alloc memory for Triset\n");
		return false;
		}

	if(newtriset->Create(GlobalRefSceneMgr)==false)
		{
		newtriset->Drop();
		MStdPrintf("**Unable to load triset %d\n",trisetid);
		return false;
		}

	if(GlobalTrisetList.Add(trisetid,newtriset)==false)
		{
		newtriset->Drop();
		return false;
		}
	
	// Drop Reference to texture
	newtriset->Drop();
	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLTrisetExists(int trisetid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	return GlobalTrisetList.IsKey(trisetid);		
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLTrisetUnload(int trisetid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTrisetList.Remove(trisetid)==false)
		{
		return false;
		}
	
	return true;
	}


/////////////////////////////////////////////////
MDLLFUN bool CGLTrisetUnloadAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTrisetList.Clear()==false)
		{
		return false;
		}

	return true;	
	}


/////////////////////////////////////////////////
MDLLFUN int CGLTrisetGetCount(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}

	return GlobalTrisetList.GetCount();
	}


////////////////////////////////////////////////
MDLLFUN int CGLTrisetAddNode(int trisetid,int nodeid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}

	MCGLTriangleSelectorInfo *trisetinfo=GlobalTrisetList.Find(trisetid);
	if(trisetinfo==NULL)
		{
		MStdPrintf("**Unable to find triset %d\n",trisetid);
		return false;
		}

	MCGLNodeInfo *nodeinfo=GlobalNodeList.Find(nodeid);
	if(nodeinfo==NULL)
		{
		MStdPrintf("**Unable to find node %d\n",nodeid);
		return false;
		}

	if(trisetinfo->Add(nodeinfo)==false)
		{
		MStdPrintf("**Unable to add node %d to triset %d\n",nodeid,trisetid);
		return false;
		}
	
	return true;
	}

